import 'package:card_swiper/card_swiper.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_syt/routes.all.dart';
import 'package:flutter_syt/theme/theme_color.dart';
import 'package:yyy_route_annotation/yyy_route_annotation.dart';

class ScrollerCover extends StatefulWidget {
  final int length;
  final Widget Function(BuildContext context, int index) builder;
  final Widget Function(int idx, int count)? indicatorBuilder;
  final double height;
  final bool loop;
  final bool autoplay;
  final SwiperController? controller;
  final EdgeInsetsGeometry margin;

  ScrollerCover(
    this.length,
    this.builder, {
    this.indicatorBuilder,
    this.height = 375,
    this.loop = true,
    this.autoplay = true,
    this.controller,
    this.margin = const EdgeInsets.only(bottom: 5),
  });

  @override
  State<StatefulWidget> createState() => ScrollerCoverState();
}

class ScrollerCoverState extends State<ScrollerCover> {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: widget.height,
      width: double.infinity,
      child: Swiper(
        autoplayDelay: 8000,
        loop: widget.loop,
        controller: widget.controller,
        containerHeight: widget.height,
        containerWidth: double.infinity,
        itemBuilder: (BuildContext context, int index) {
          return widget.builder(context, index);
        },
        itemCount: widget.length,
        autoplay: widget.autoplay,
        pagination: SwiperPagination(
          margin: widget.margin,
          alignment: Alignment.bottomRight,
          builder: SwiperCustomPagination(builder: (ctx, config) {
            if (widget.indicatorBuilder != null) {
              return widget.indicatorBuilder!(
                  config!.activeIndex ?? 0, widget.length);
            }

            return _Indicator(config!.activeIndex ?? 0, widget.length);
          }),
        ),
      ),
    );
  }
}

class _Indicator extends StatelessWidget {
  final int _currentIndex;
  final int _count;

  _Indicator(this._currentIndex, this._count);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.only(top: 5),
      margin: EdgeInsets.only(right: 34, top: 10),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        children: List.generate(_count, (index) {
          return _currentIndex == index
              ? Container(
                  width: 10,
                  height: 2,
                  margin: EdgeInsets.only(right: 8),
                  decoration: BoxDecoration(
                      color: ThemeColor.primaryColor,
                      borderRadius: BorderRadius.circular(1)),
                )
              : Container(
                  width: 10,
                  height: 2,
                  margin: EdgeInsets.only(right: 8),
                  decoration: BoxDecoration(
                      color: Color(0xffffff).withOpacity(0.5),
                      borderRadius: BorderRadius.circular(20)),
                );
        }),
      ),
    );
  }
}

class CenterIndicator extends StatelessWidget {
  final int _currentIndex;
  final int _count;

  CenterIndicator(this._currentIndex, this._count);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(top: 10),
      child: Container(
        decoration: BoxDecoration(
          color: Color(0xFFF0F0F0),
          borderRadius: BorderRadius.circular(1.5),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          mainAxisSize: MainAxisSize.min,
          children: List.generate(_count, (index) {
            return _currentIndex == index
                ? Container(
                    width: 10,
                    height: 3,
                    decoration: BoxDecoration(
                        color: ThemeColor.primaryColor,
                        borderRadius: BorderRadius.circular(1.5)),
                  )
                : Container(
                    width: 10,
                    height: 3,
                    decoration: BoxDecoration(
                        color: Color(0xFFF0F0F0),
                        borderRadius: BorderRadius.circular(1.5)),
                  );
          }),
        ),
      ),
    );
  }
}

class CircleIndicator extends StatelessWidget {
  final int _currentIndex;
  final int _count;

  CircleIndicator(this._currentIndex, this._count);

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.bottomRight,
      margin: EdgeInsets.only(top: 0, right: 27.w, bottom: 2.w),
      child: Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(2.5),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.end,
          mainAxisSize: MainAxisSize.min,
          children: List.generate(_count, (index) {
            return _currentIndex == index
                ? Container(
                    width: 10,
                    height: 3,
                    margin: const EdgeInsets.only(left: 5),
                    decoration: BoxDecoration(
                        color: ThemeColor.primaryColor,
                        borderRadius: BorderRadius.circular(1.5)),
                  )
                : Container(
                    width: 10,
                    height: 3,
                    margin: const EdgeInsets.only(left: 5),
                    decoration: BoxDecoration(
                        color: Colors.white.withOpacity(0.2),
                        borderRadius: BorderRadius.circular(1.5)),
                  );
          }),
        ),
      ),
    );
  }
}
